package com.weimai.message.gateway.controller;

import com.weimai.message.gateway.anno.RouteHandler;
import com.weimai.message.gateway.anno.RouteMapping;
import com.weimai.message.gateway.anno.RouteMethod;
import com.weimai.message.gateway.model.ReplyObj;
import com.weimai.message.gateway.utils.AsyncServiceUtil;
import com.weimai.message.gateway.utils.HttpUtil;
import com.weimai.message.gateway.utils.ParamUtil;
import com.weimai.message.gateway.serviceasync.SmsAsyncService;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;

/**
 * 描述：短信消息
 * Copyright (c) 2018 Choice, Inc.
 * All Right Reserved.
 * Choice Proprietary and Confidential.
 *
 * @Author geer
 * @Since 2018/11/12
 * @Version 1.0
 **/
@RouteHandler("msg/v1/sms")
@Slf4j
public class SmsController {
    
    private SmsAsyncService smsAsyncService = AsyncServiceUtil.getAsyncServiceInstance(SmsAsyncService.class);
    
    
    @RouteMapping(value = "/send/text", method = RouteMethod.POST)
    public Handler<RoutingContext> sendText() {
        
        return (RoutingContext event) -> {
            String phoneNumbers = null;
            String smsContent = null;
            String accountNo = null;
            JsonObject param = ParamUtil.getRequestParams(event);
            
            try{
                if (param.containsKey("phoneNumbers")) {
                    phoneNumbers = param.getString("phoneNumbers");
                    param.put("phoneNumbers", phoneNumbers);
                } else {
                    HttpUtil.fireJsonResponse(event.response(), HTTP_INTERNAL_ERROR,
                            ReplyObj.build().setData("缺少参数：phoneNumbers").setCode(HTTP_INTERNAL_ERROR));
                }
    
                if (param.containsKey("smsContent")) {
                    smsContent = param.getString("smsContent");
                    param.put("smsContent", smsContent);
                } else {
                    HttpUtil.fireJsonResponse(event.response(), HTTP_INTERNAL_ERROR,
                            ReplyObj.build().setData("缺少参数：smsContent").setCode(HTTP_INTERNAL_ERROR));
                }
    
                if (param.containsKey("accountNo")) {
                    accountNo = param.getString("accountNo");
                    param.put("accountNo", accountNo);
                } else {
                    HttpUtil.fireJsonResponse(event.response(), HTTP_INTERNAL_ERROR,
                            ReplyObj.build().setData("缺少参数：accountNo").setCode(HTTP_INTERNAL_ERROR));
                }
            }catch (Exception e){
                HttpUtil.fireJsonResponse(event.response(), HTTP_INTERNAL_ERROR,
                        ReplyObj.build().setData("请求参数错误！").setCode(HTTP_INTERNAL_ERROR));
            }

            
            smsAsyncService.sendSms(phoneNumbers, smsContent, accountNo, ar -> {
                if (ar.succeeded()) {
                    String msgId = ar.result();
                    HttpUtil.fireJsonResponse(event.response(), HTTP_OK, ReplyObj.build().setData(msgId));
                } else {
                    HttpUtil.fireJsonResponse(event.response(), HTTP_INTERNAL_ERROR,
                            ReplyObj.build().setData(ar.cause().getMessage()).setCode(HTTP_INTERNAL_ERROR));
                }
            });
        };
    }
    
}
